home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / comm2 / pop3get.lha / pop3get.c < prev    next >
C/C++ Source or Header  |  1995-08-21  |  4KB  |  210 lines

  1. /*
  2. **    POP3GET.c
  3. **
  4. **  Simple POP3 Get utility
  5. **
  6. **  Requires "RMail" to be in the path and executable (tested
  7. **  with ASMail only)
  8. **
  9. **  (C) 1995 Oliver Wagner (owagner@lsd.wupper.de),
  10. **  Public Domain
  11. **
  12. **  No warranties.
  13. **
  14. **  To be compiled with SAS/C 6.50.
  15. **
  16. */
  17.  
  18. #define __USE_SYSBASE
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <dos/dostags.h>
  22. #include <dos/stdio.h>
  23. #include <string.h>
  24.  
  25. #include "rev.h"
  26.  
  27. #define min __builtin_min
  28.  
  29. char version[] = { "$VER: Pop3Get " VERTAG };
  30.  
  31. struct myargs {
  32.     char *host;
  33.     char *port;
  34.     char *user;
  35.     char *pass;
  36.     ULONG nodelete;
  37. } myargs;
  38.  
  39. long __oslibversion = 37;
  40.  
  41. //    sprintf() replacement
  42. static UWORD fmtfunc[] = { 0x16c0, 0x4e75 };
  43. static void __stdargs sprintf( char *to, char *fmt, ... )
  44. {
  45.     RawDoFmt( fmt, &fmt + 1, (APTR)fmtfunc, to );
  46. }
  47.  
  48. static void clnl( char *p )
  49. {
  50.     p = stpbrk( p, "\r\n" );
  51.     if( p )
  52.         *p = 0;
  53. }
  54.  
  55. char buffer[ 512 ];
  56. BPTR sock; // :-)
  57.  
  58. // This is the tricky one
  59. static int doxfer( void )
  60. {
  61.     int c;
  62.     int len;
  63.     BPTR f;
  64.     char *tmpname;
  65.     char toline[ 512 ], *top;
  66.  
  67.     for( c = 1; ; c++ )
  68.     {
  69.         int inheader = TRUE;
  70.         char *p;
  71.  
  72.         top = toline;
  73.         *top = 0;
  74.  
  75.         FPrintf( sock, "RETR %ld\n", c );
  76.         if( !FGets( sock, buffer, sizeof( buffer ) ) )
  77.             return( 10 );
  78.         if( strnicmp( buffer, "+OK", 3 ) )
  79.             break;
  80.         len = atoi( &buffer[ 4 ] );
  81.         Printf( "Retrieving mail #%ld, %ld bytes...\n", c, len );
  82.  
  83.         tmpname = len > 32768 ? "pop3get.tmp" : "T:pop3get.tmp";
  84.  
  85.         f = Open( tmpname, MODE_NEWFILE );
  86.         if( !f )
  87.         {
  88.             Printf( "*ALERT* Unable to open \"%s\"; IoErr = %ld, exiting...\n", tmpname, IoErr() );
  89.         }
  90.         SetVBuf( f, NULL, BUF_FULL, 8192 );
  91.         for(;;)
  92.         {
  93.             if( !FGets( sock, buffer, sizeof( buffer ) ) )
  94.             {
  95.                 Printf( "*ALERT* Receive error during RETR!\n" );
  96.                 Close( f );
  97.                 DeleteFile( tmpname );
  98.                 return( 10 );
  99.             }
  100.  
  101.             clnl( buffer );
  102.             if( !strcmp( buffer, "." ) )
  103.                 break;    // EOM
  104.             FWrite( f, buffer, strlen( buffer ), 1 );
  105.             FPutC( f, '\n' );
  106.  
  107.             if( !buffer[ 0 ] )
  108.                 inheader = FALSE;
  109.  
  110.             if( inheader )
  111.             {
  112.                 if( !strnicmp( buffer, "To:", 3 ) )
  113.                 {
  114.                     p = strtok( buffer, " ,\t<>" );
  115.                     while( p )
  116.                     {
  117.                         char *p2;
  118.                         p2 = strchr( p, '@' );
  119.                         if( p2 )
  120.                         {
  121.                             char *p3 = p;
  122.                             while( *p3 != '@' )
  123.                                 *top++ = *p3++;
  124.                             *top++ = ' ';
  125.                         }
  126.                         else
  127.                         {
  128.                             p2 = strchr( p, '!' );
  129.                             if( p2 )
  130.                             {
  131.                                 strcpy( top, p2 + 1 );
  132.                                 top = strchr( top, 0 );
  133.                                 *top++ = ' ';
  134.                             }
  135.                         }
  136.                         p = strtok( NULL, " ,\t" );
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.  
  142.         if( top > toline )
  143.             top[ - 1 ] = 0;
  144.         else
  145.             strcpy( toline, "No-To-Found" );
  146.  
  147.         Close( f );
  148.         Printf( "Msg #%ld to \"%s\", sending...\n", c, toline );
  149.         sprintf( buffer, "RMail <%s %s", tmpname, toline );
  150.         SystemTags( buffer, TAG_DONE );
  151.         DeleteFile( tmpname );
  152.  
  153.         if( !myargs.nodelete )
  154.         {
  155.             Printf( "Deleting message #%ld...\n", c );
  156.             FPrintf( sock, "DELE %ld\n", c );
  157.             FGets( sock, buffer, sizeof( buffer ) );
  158.         }
  159.     }
  160.     Printf( "finished transaction; got %ld mails total.\n", c - 1 );
  161.     return( c == 1 ? 5 : 0 );
  162. }
  163.  
  164. void __stdargs __main( char *dummy )
  165. {
  166.     struct RDArgs *rda;
  167.     int rc = 10;
  168.  
  169.     Printf( "%s written by Oliver Wagner (owagner@lsd.wupper.de)\n", &version[ 6 ] );
  170.  
  171.     myargs.port = "110";
  172.     rda = ReadArgs( "Host/A,Port/K,Username=USER/A,Password=PASS/A,NoDelete/S", (LONG*)&myargs, NULL );
  173.     if( !rda )
  174.     {
  175.         PrintFault( IoErr(), "Pop3Get" );
  176.         exit( 10 );
  177.     }
  178.  
  179.     sprintf( buffer, "TCP:%s/%s", myargs.host, myargs.port );
  180.     Printf( "Attempting connection to host \"%s\" on port %s...\n", myargs.host, myargs.port );
  181.     sock = Open( buffer, MODE_NEWFILE );
  182.     if( sock )
  183.     {
  184.         int okcount = 0;
  185.  
  186.         PutStr( "Connection established, now doing login.\n" );
  187.         FPrintf( sock, "USER %s\nPASS %s\n", myargs.user, myargs.pass );
  188.  
  189.         // We're looking for the third OK (nifty, eh? :-)
  190.         while( okcount < 3 && FGets( sock, buffer, sizeof( buffer ) ) )
  191.         {
  192.             clnl( buffer );
  193.             Printf( "got: '%s'\n", buffer );
  194.             if( !strnicmp( buffer, "+OK", 3 ) )
  195.                 okcount++;
  196.             else if( !strnicmp( buffer, "-ERR", 4 ) )
  197.                 break;
  198.         }
  199.         if( okcount == 3 )
  200.         {
  201.             rc = doxfer();
  202.             FPrintf( sock, "QUIT\n" );
  203.         }
  204.     }
  205.     else
  206.         Printf( "Unable to connect, error %ld.\n", IoErr() );
  207.     FreeArgs( rda );
  208.     exit( rc );
  209. }
  210.